/* ********************************************************************************************
/* ********************************************************************************************
								Windows Design 
/* ********************************************************************************************
/* ******************************************************************************************** */

/* ********************************************************************************************
/* ********************************************************************************************
								Date Validation 
/* ********************************************************************************************
/* ******************************************************************************************** */
/*	===================================
	Purpose		: Get Day name in a week
	Function	: GetDay
	Date		: 31/03/2008
	=================================== */
function GBLVdn_GetDay(nDay)
{
	var Days = new Array("Sunday","Monday","Tuesday","Wednesday",
	                     "Thursday","Friday","Saturday");
	return Days[nDay]
}
/*	===================================
	Purpose		: Get Month Name in a year
	Function	: GetMonth
	Date		: 31/03/2008
	=================================== */
function GBLVdn_GetMonth(nMonth)
{
	var Months = new Array("January","February","March","April","May","June",
	                       "July","August","September","October","November","December");
	return Months[nMonth] 	  	 
}
/*	===================================
	Purpose		: Get Date,Month & Year
	Function	: GBLVdn_GetDMY
	Date		: 31/03/2008
	=================================== */
function GBLVdn_GetDMY(l_sDate,l_DMY_ID,l_splitChar)
{
	// l_DMY_ID ->0-Date,1-Month & 2-Year
	if (l_splitChar == "" || l_splitChar == "none" )
		l_splitChar = "/";
	var dateValArray = l_sDate.split(l_splitChar);
	return dateValArray[l_DMY_ID];
}
/*	===================================
	Purpose		: Get Date in String Format
	Function	: DateString
	Date		: 31/03/2008
	=================================== */
function GBLVdn_DateString()
{
	var Today = new Date();
	var suffix = "th";
	switch (Today.getDate())
	{
		case 1:
		case 21:
		case 31: 
			suffix = "st"; break;
		case 2:
		case 22:
			suffix = "nd"; break;
		case 3:
		case 23:
			suffix = "rd"; break;
	};

	var strDate = GetDay(Today.getDay()) + " " + Today.getDate();
	strDate += suffix + " " + GetMonth(Today.getMonth()) + ", " + Today.getYear();
	return strDate
}
/*	===================================
	Purpose		: Get Days in Given Month
	Function	: GBLVdn_monthDaysCount
	Date		: 31/03/2008
	=================================== */
function GBLVdn_monthDaysCount(l_month, l_year)
{
	var l_TotDays = 0;
	l_month = eval(l_month);
	switch(l_month)
	{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12: l_TotDays =31; break;
		case 2: 
			if(l_year%4 == 0)
				l_TotDays =29; 
			else
				l_TotDays = 28;
			break;
		case 4:
		case 6:
		case 9:
		case 11:l_TotDays=30;break;
		}
		return l_TotDays;
}
/*	===================================
	Purpose		: Compare Two Dates
	Function	: CompareDates
	Date		: 31/03/2008
	=================================== */
function GBLVdn_CompareDates(fromDate,toDate) // 31/03/2008
{
    var str1  = fromDate;
    var str2  = toDate;
    var dt1   = parseInt(str1.substring(0,2),10);
    var mon1  = parseInt(str1.substring(3,5),10);
    var yr1   = parseInt(str1.substring(6,10),10);
    var dt2   = parseInt(str2.substring(0,2),10);
    var mon2  = parseInt(str2.substring(3,5),10);
    var yr2   = parseInt(str2.substring(6,10),10);
    var date1 = new Date(yr1, mon1, dt1);
    var date2 = new Date(yr2, mon2, dt2);

    if(date2 < date1)
        return 2; // Less
    else if(date2 > date1)
        return 1; // Greater 
    else
		return 0; // Equal
}

/*	===================================
	Purpose		: Compare Two Dates & Display Error Message
	Function	: GBLVdn_CompareDates
			fromDatectrlname - > First Date Ctrl Name,
			toDateObj	-> 2nd ctrl Object (this)
			conid -> Condition Id (0,1,2)
			errid -> 0-Don't Show Error, 1-Show Error Popup
	Date		: 31/03/2008
	=================================== */
function GBLVdn_MsgCompareDates(fromDatectrlname,toDateObj,conid,errid) 
{
	if ( fromDatectrlname == null || fromDatectrlname == "" ) return;
	var fromDateObj = document.getElementById(fromDatectrlname);
	if ( errid == 0 )
		toDateObj = document.getElementById(toDateObj);
	if ( fromDateObj == null || toDateObj == null ) return;
	var  sErrMsg= "";
	var fromDate = GBLVdn_Trim(fromDateObj.value);
	var toDate = GBLVdn_Trim(toDateObj.value);

	if ( fromDate == null || toDate == null ) return;
	if ( fromDate == "" || toDate == "" ) return;	
	
	if ( errid == 0 )
	{
		return GBLVdn_CompareDates(fromDate,toDate);
	}
	
	if ( GBLVdn_CompareDates(fromDate,toDate) == 1 && conid == 1 )
	{
		sErrMsg = toDate +" is Greater Than "+ fromDate;
		sErrMsg += 'Please correct.'
	}
	else if ( GBLVdn_CompareDates(fromDate,toDate) == 2  && conid == 2 )
	{
		sErrMsg = toDate +" is Less Than "+ fromDate;
		sErrMsg += 'Please correct.'
	}
	else if ( GBLVdn_CompareDates(fromDate,toDate) == 0  && conid == 0 )
	{
		sErrMsg = toDate +" is Equal "+ fromDate;
		sErrMsg += 'Please correct.'
	}
	
	if ( sErrMsg != "" )
	{
		inlineMsg(toDateObj,sErrMsg,2);
		toDateObj.focus();
	}
}

/*	===================================
	Purpose		: Check Valid date
	Function	: GBLVdn_ValidateDate
	Date		: 31/03/2008
	=================================== */
function GBLVdn_ValidateDate(pFieldobj,pMsgid)
{
	if ( pFieldobj == null ) return '';
	//alert(pFieldobj.value);
	var sDate = GBLVdn_Trim(pFieldobj.value);
	if ( sDate == "" || sDate.length == 0 ) return '';
    var validformat=/^\d{1,2}\/\d{1,2}\/\d{4}$/ //Basic check for format validity
    var returnval=false
    if (!validformat.test(pFieldobj.value))
    {
		if ( pMsgid == 1 ) // Show Error Popup
		{
			inlineMsg(pFieldobj,'Invalid Date Format(dd/mm/yyyy). Please correct.',2);
			pFieldobj.focus();
		}
		return 'Invalid Date Format(dd/mm/yyyy). Please correct.';
	}
    else
	{ //Detailed check for valid date ranges
		var dayfield=pFieldobj.value.split("/")[0]
		var monthfield=pFieldobj.value.split("/")[1]
		var yearfield=pFieldobj.value.split("/")[2]
	   
		var dayobj = new Date(yearfield, monthfield-1, dayfield)
		if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
		{
			if ( pMsgid == 1 ) // Show Error Popup
			{
				inlineMsg(pFieldobj,'Invalid Day, Month, or Year range detected. Please correct.',2);
				pFieldobj.focus();
			}
			return 'Invalid Day, Month, or Year range detected. Please correct.'
		}
	}
		
    return '';
} 

/*	===================================
	Purpose		: Enter Number only & / auto dispaly
	Function	: GBLVdn_AutoDateSlash
	Date		: 31/03/2008
	=================================== */
function GBLVdn_AutoDateSlash(evt,ctrlobj) 
{
	var l_keyCode=(window.event)?evt.keyCode:evt.which;
	if ( l_keyCode == 8 ||(l_keyCode == 13 || l_keyCode <=0) )//8->back space,13-enter
		return true;
		/*	case 8:     // backspace		case 37:    // left arrow
			case 39:    // right arrow		case 46:    // delete		*/
	var sExtVal = ctrlobj.value;
	if ( sExtVal.length >= 10 ) return false;
	if( (Number(l_keyCode)>47) && (Number(l_keyCode)<=57) )  
	{
		//alert(sExtVal.length);
		if ( Number(sExtVal.length) == 0 ) // Date start 0,1,2,3
		{	if ( Number(l_keyCode) > 51 ) { l_keyCode=0; return false; } }
		if ( Number(sExtVal.length == 1) ) // Date 2nd cahr not >2 when ist char 3
		{	if ( Number(ctrlobj.value) == 3 && Number(l_keyCode) > 49 )	{ l_keyCode=0; return false; }	}
		
		if ( Number(sExtVal.length) == 3 ) // Month start 0,1
		{	if ( Number(l_keyCode) > 49 ) { l_keyCode=0; return false; } }
		if ( Number(sExtVal.length) == 4 ) // Month 2nd 0,1,2
		{	if ( Number(l_keyCode) > 50 ) { l_keyCode=0; return false; } }
		
		if ( Number(sExtVal.length) == 1 ||  Number(sExtVal.length) == 4 )
		{
			ctrlobj.value = sExtVal + String.fromCharCode( l_keyCode ) + "/"; 
			l_keyCode=0; return false;
		}
		return true;
	}
	else if( Number(l_keyCode) == 47 ) 
	{
		if ( Number(sExtVal.length) == 2 ||  Number(sExtVal.length) == 5 ) // 3rd & 5th position have / (dd/mm/yyyy)
			return true;
		l_keyCode=0;
	}
	else { l_keyCode=0; }
	return false;
}
